package com.mycompony.shopping.shangguan.sys.interceptor;

import org.apache.struts2.dispatcher.SessionMap;

import com.mycompony.shopping.shangguan.sys.model.User;
import com.mycompony.shopping.shangguan.sys.util.Constant;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class AuthenticationInterceptor implements Interceptor {

	private static final long serialVersionUID = -4404189799938710187L;

	private static final Logger LOG = LoggerFactory.getLogger(AuthenticationInterceptor.class);

	public void destroy() {

	}

	public void init() {

	}

	public String intercept(ActionInvocation invocation) throws Exception {

		LOG.debug("Authenticating chat user");

		SessionMap session = (SessionMap) ActionContext.getContext().get(ActionContext.SESSION);

		User user = (User) session.get(Constant.USER_SESSION_KEY);

		if (user == null) {
			return Action.LOGIN;
		}

		return invocation.invoke();

	}

}
